<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input class="password-input" v-model="user.oldPassword" placeholder="请输入旧密码" type="text" onkeyup="value=value.replace(/[\u4E00-\u9FA5]/g,'')"/>
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input class="password-input" v-model="user.newPassword" placeholder="请输入新密码" type="text" onkeyup="value=value.replace(/[\u4E00-\u9FA5]/g,'')"/>
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input class="password-input" v-model="user.confirmPassword" placeholder="请确认新密码" type="text" onkeyup="value=value.replace(/[\u4E00-\u9FA5]/g,'')"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserPwd } from "@/api/system/user";

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    // 密码校验 长度不能小于8位且不能大于20位字符,必须包含大写字母、小写字母、数字和特殊符号
    const ISPWD =/^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*,\._\+(){}])[0-9a-zA-Z!@#$%^&*,\\._\+(){}]{8,20}$/;
    const validatePassword = (rule, value, callback) =>{
      if (!ISPWD.test(this.user.newPassword)) {
        // callback(new Error("用户密码必须包含大写字母、小写字母、数字和特殊符号"));
        callback(new Error("密码中必须包含大写字母、小写字母、数字和特殊符号，长度在8-20位之间！"));

      } else {
        callback();
      }
    };
    return {
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" }
        ],
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          { min: 8, max: 20, message: '用户密码长度必须介于 8 和 20 之间', trigger: 'blur' },
          { required: true, validator: validatePassword, trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>


<style lang="scss" scoped>
.password-input {
  position: relative;
  font-size: 14px; /* 调整字体大小以适配隐藏文本 */
  color: transparent; /* 将文本颜色设为透明 */
  letter-spacing: 0.25em; /* 调整字符间距以隐藏文本 */
  -webkit-text-security: disc; /* 将输入内容显示为圆点，适用于部分浏览器 */
}
</style>